define([], function() {
	return {
		title: ['<a href="user.html">个人中心</a>', "摄影管理", "上传图片"],
		init: function(content) {
			var form = $("form");
			var imgFile = form.find(":file[name='img']");
			//console.info(imgFile);
			var img = $("#imgShow");
			$.get(
				"photo/picType.do",
				function(data) {
					//console.info(data);
					$.each(data, function(i, v) {
						$("<option></option>")
						.val(v.photoPicClassId)
						.text(v.photoPicClassName)
						.appendTo($("#albumId"));
					});
					
				},"json"
			);
			imgFile.on("change", function() {
				var f = imgFile[0].files[0];
				console.info(f);
				var reader = new FileReader();
				reader.onload = function() {
					img[0].src = reader.result;
				}
				reader.readAsDataURL(f);
			});
			var progress = $(".progress");
			
			form.on("submit", function() {
				let formData = new FormData(form[0]);
			
				progress.show();
			
				$.ajax({
					type: "post",
					url: "photo/upload-pic.do",
					processData: false, // 不会将 data 参数序列化字符串
					contentType: false, // 根据表单 input 提交的数据使用其默认的 contentType
					data: formData,
					success: function(data) {
						if(data.ok) {
							toastr.success("上传成功");
							form.trigger("reset");
						} else {
							toastr.error(data.message);
						}
						progress.hide();
					},
					dataType: "json",
					xhr: function() {
						var xhr = new window.XMLHttpRequest();
						xhr.upload.addEventListener("progress", function(evt) {
							if(evt.lengthComputable) {
								var percentComplete = evt.loaded * 100 / evt.total;
								progress.children().css("width", percentComplete + "%");
							}
						}, false);
						return xhr;
					}
				});
			
				return false;
			});
			
			form.on("reset", function() {
				img[0].src = "img/_noimg.png";
			});
			
		}
	}
});